<template>
    <SysDialog :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
        @onClose="onClose" @onConfirm="commit">
        <template v-slot:content>
            <el-form :model="addModel" ref="addFormRef" label-width="90px" :inline="false"
                size="default">
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="name" label="姓名">
                            <el-input v-model="addModel.name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="sex" label="性别">
                            <el-input v-model="addModel.sex"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="birthday" label="生日">
                         <el-date-picker v-model="addModel.birthday" placeholder="请输入生日" style="width: 100%;"</el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="fuZeQuYu" label="负责区域">
                            <el-input v-model="addModel.fuZeQuYu"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="zhiWu" label="职务">
                            <el-input v-model="addModel.zhiWu"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="lianXiFangShi" label="联系方式">
                            <el-input v-model="addModel.lianXiFangShi"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-form-item prop="beiZhu" label="备注">
                     <textarea  rows="5" cols="100" v-model="addModel.beiZhu"></textarea>
                </el-form-item>
                 
            </el-form>

        </template>
    </SysDialog>
</template>

<script setup lang="ts">

import SysDialog from '@/components/SysDialog.vue'
import useDialog from '@/hooks/useDialog';
import { nextTick, reactive, ref } from 'vue';
import { YxryModel } from '@/api/yxry/YxryModel';
import { ElMessage, FormInstance } from 'element-plus';
import { addYxryApi, editYxryApi, getListApi } from '@/api/yxry';
import { EditType, Title } from '@/type/BaseType';
const { dialog, onClose, onConfirm, onShow } = useDialog()    
const show = (type: string, row?: YxryModel) => {
    
    type == EditType.ADD ? dialog.title = Title.ADD : dialog.title = Title.EDIT
    dialog.height = 500
    dialog.width = 1000
    dialog.visible = true

    if (row) {
        nextTick(() => {
            Object.assign(addModel, row)
            console.log('addModel:', addModel);
        })
    }

    addFormRef.value?.resetFields()

    addModel.type = type;
};

defineExpose({
    show
})

const addModel = reactive<YxryModel>({
    type: '',
    id: '',
    name:'',
    sex:'',
    birthday:'',
    fuZeQuYu:'',
    zhiWu:'',
    lianXiFangShi:'',
    beiZhu:'',
    
})

const addFormRef = ref<FormInstance>()


const emists = defineEmits(['onFresh'])  


const commit = () => {
    addFormRef.value?.validate(async (valid) => {
        if (valid) {
            let res = null;
            if (addModel.type == EditType.ADD) {
                res = await addYxryApi(addModel);
            } else {
                res = await editYxryApi(addModel);
            }
            console.log(res)
            if (res && res.code == 200) {
                ElMessage.success(res.msg)
                emists('onFresh')
                onClose()
            }
        }
    })
}
</script>
<style scoped></style>